package com.gitee.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.gitee.domain.system.dto.LoginBody;
import com.gitee.service.LoginService;
import com.gitee.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName：AuthController
 * @Author: bin
 * @Date: 2024/11/2 16:00
 * @Description: 认证
 */
@Slf4j
@RequestMapping("/auth")
@RestController
public class AuthController {

    @Autowired
    private LoginService loginService;

    /**
     * 登录方法
     *
     * @param loginBody
     * @return
     */
    @PostMapping("/login")
    public Result<String> login(@RequestBody LoginBody loginBody) {
        if (loginBody.getUsername() == null || loginBody.getPassword() == null) {
            return Result.failed("账号名或密码不能为空");
        }
        // 校验用户账号密码
        loginService.checkLogin(loginBody);
        StpUtil.login(loginBody.getUsername());
        String token = StpUtil.getTokenValue();
        return Result.success(token,"获取token成功");
    }
}
